import 'package:flutter/material.dart';
import 'package:flutter_audit_app/res/colors.dart';
import 'package:flutter_audit_app/res/strings.dart';
import 'package:flutter_audit_app/ui/home/home_page.dart';
import 'package:flutter_audit_app/ui/mine/mine_page.dart';
import 'package:flutter_audit_app/ui/msg/msg_page.dart';
import 'package:flutter_audit_app/ui/stat/stat_page.dart';

import 'bottom_bar_widget.dart';

class NavBarController extends StatefulWidget {
  final index;

  NavBarController({Key key, this.index = 0}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _NavBarControllerState();
}

class _NavBarControllerState extends State<NavBarController> {
  GlobalKey<StackWidgetState> _stackGk = GlobalKey<StackWidgetState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: [
          BotNavBarWidget(
            stackValue: (int currentIndex, List<int> tabInt) {
              _stackGk.currentState.changeStack(currentIndex, tabInt);
            },
          ),
          Expanded(
            child: StackWidget(key: _stackGk),
          ),
        ],
      ),
    );
  }
}

class StackWidget extends StatefulWidget {
  StackWidget({Key key, this.currentIndex = 0, this.tabInt}) : super(key: key);

  final int currentIndex;
  final List<int> tabInt;

  @override
  State<StatefulWidget> createState() => StackWidgetState();
}

class StackWidgetState extends State<StackWidget> {
  int _currentIndex = 0;
  List<int> _tabInt;

  final List<Widget> _children = [
    HomePage(), //主页
    MsgPage(), //消息
    StatPage(), //统计
    MinePage() //我的
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabInt = widget.tabInt == null ? [0] : widget.tabInt;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      child: _stack(),
    );
  }

  void changeStack(int currentIndex, List<int> tabInt) {
    setState(() {});
    _currentIndex = currentIndex;
    _tabInt = tabInt;
  }

  Stack _stack() {
    return Stack(
      children: <Widget>[
        _child(0),
        _child(1),
        _child(2),
        _child(3),
      ],
    );
  }

  //Page的显示和隐藏
  _child(int _index) {
    return Offstage(
      offstage: !(_currentIndex == _index),
      child: _tabInt.contains(_index) ? _children[_index] : Container(),
    );
  }
}

///处理页面切换时反复销毁重建的问题
class BotNavBarWidget extends StatefulWidget {
  final ValueChanged stackValue;

  BotNavBarWidget({Key key, this.stackValue}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _BotNavBarWidgetState();
}

class _BotNavBarWidgetState extends State<BotNavBarWidget> {
  List<int> tabInt = [0];
  int _currentIndex = 0;

  final List<BottomNavigationBarItem> navList = [
    BottomNavigationBarItem(
      icon: Image.asset(
        'assets/images/nav_home_normal.png',
        width: width(44),
        height: height(44),
      ),
      activeIcon: Image.asset(
        'assets/images/nav_home_light.png',
        width: width(44),
        height: height(44),
      ),
      title: Text(Strings.navHomeTxt),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(
        'assets/images/nav_msg_normal.png',
        width: width(44),
        height: height(44),
      ),
      activeIcon: Image.asset(
        'assets/images/nav_msg_light.png',
        width: width(44),
        height: height(44),
      ),
      title: Text(Strings.navMsgTxt),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(
        'assets/images/nav_stat_normal.png',
        width: width(44),
        height: height(44),
      ),
      activeIcon: Image.asset(
        'assets/images/nav_stat_light.png',
        width: width(44),
        height: height(44),
      ),
      title: Text(Strings.navStatTxt),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(
        'assets/images/nav_mine_normal.png',
        width: width(44),
        height: height(44),
      ),
      activeIcon: Image.asset(
        'assets/images/nav_mine_light.png',
        width: width(44),
        height: height(44),
      ),
      title: Text(Strings.navMineTxt),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 75,
      color: Colors.blue,
      child: Column(
        children: [
          SizedBox(height: 35),
          Container(
            width: width(100),
            height: height(100),
            child: CircleAvatar(
                backgroundImage: AssetImage('assets/images/admin_avatar.png')),
          ),
          Text(
            Strings.welcomeAdminTxt,
            style: TextStyle(color: Colors.white, fontSize: 14),
          ),
          SizedBox(height: 30),
          Expanded(
            child: BottomBarWidget(
              items: this.navList,
              elevation: 0.0,
              type: BottomNavigationBarType.fixed,
              backgroundColor: Colors.blue,
              //可以有多个按钮
              currentIndex: _currentIndex,
              selectedItemColor: Colors.white,
              unselectedItemColor: Colors.black38,
              onTap: onTabTapped,
            ),
          ),
        ],
      ),
    );
  }

  onTabTapped(int index) {
    setState(() {});
    _currentIndex = index;
    if (!tabInt.contains(index)) {
      tabInt.add(index);
    }
    if (widget.stackValue != null) {
      widget.stackValue(_currentIndex, tabInt);
    }
  }
}

typedef ValueChanged = void Function(
  int currentIndex,
  List<int> tabInt,
);
